package com.lxp.tksys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.tksys.common.R;
import com.lxp.tksys.entity.Employee;
import com.lxp.tksys.entity.Orders;
import com.lxp.tksys.entity.User;
import com.lxp.tksys.service.OrderService;
import com.lxp.tksys.service.UserService;
import com.lxp.tksys.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")   //用户已经登录，随时从session中拿id，有了id就有了菜品数据
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据:{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")     //user页面分页展示订单
    public R<Page> userPage(int page, int pageSize){
        //log.info("page={},pageSize={},name={}",page,pageSize,name);

        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @GetMapping("/page")   //管理后台分页展示订单数据
    public R<Page> page(int page, int pageSize, String number,String beginTime,String endTime){
        //log.info("page={},pageSize={},name={}",page,pageSize,name);

        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(number!=null,Orders::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);
        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }


}
